/**
 * 
 */

var xmlDoc = null, xmlhttp = null;
var xmlFile = "javascript/MyXml.xml?";
function LoadXML() {
	xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest()
			: new ActiveXObject("Microsoft.XMLHttp");
	if (xmlhttp == null) {
		alert("你的浏览器不支持 XMLHttpRequest");
		return;
	}
	xmlhttp.open("GET", xmlFile, true);
	xmlhttp.setRequestHeader("Content-Type", "text/xml");
	xmlhttp.onreadystatechange = ReadXML;
	xmlhttp.send(null);
}

function UnitExist(unitname) {
	var bool = false;
	var t = document.getElementById(unitname);
	if (t != null) {
		bool = true;
	}
	return bool;
}

function ReadXML() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var xmlDoc = xmlhttp.responseXML.documentElement;
		if (xmlDoc == null) {
			alert("返回的数据不正确。");
			return;
		}
		var chartType = null;
		var chartSelect = document.getElementsByName("chart");
		var b = false;
		for (i = 0; i < chartSelect.length; i++) {
			if (chartSelect[i].checked == true) {
				chartType = chartSelect[i].value;
				b = true;
			}
		}
		if (b == false)
			return;
		try {
			var unitList = xmlDoc.getElementsByTagName("unit");
			for (var i = 0; i < unitList.length; i++) {
				var unitname = unitList[i].attributes[0].value;
				if (UnitExist(unitname)) {
					var dataArray = [];
					var dataList = unitList[i].getElementsByTagName("data");
					for (var j = 0; j < dataList.length; j++) {
						var name = dataList[j].getElementsByTagName("name")[0].childNodes[0].nodeValue;
						var value = dataList[j].getElementsByTagName("value")[0].childNodes[0].nodeValue;
						var dataRow = {};
						dataRow["value"] = value;
						dataRow["name"] = name;
						dataArray.push(dataRow);
					}
					CreateChart(chartType, unitname, dataArray);
				} else {
					continue;
				}
			}
		} catch (e) {
			alert(e.message);
		}
	}
}